package com.pony.iphone.controller;

import com.pony.iphone.dto.ErrorDto;
import com.pony.iphone.dto.TimeRange;
import com.pony.iphone.service.ErrorService;
import com.pony.iphone.utils.AppHttpCodeEnum;
import com.pony.iphone.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 错误收集功能（计数）
 * @author pony
 */
@Slf4j
@RestController
@RequestMapping("/error")
public class ErrorController {

    @Autowired
    private ErrorService errorService;

    /**
     * 批量错误上传
     */
    @PostMapping("/upload")
    public R batchUploadError(@RequestBody ErrorDto request) {
        try {
            log.info("接收到批量错误上传请求，数量: {}", request.getNames().size());
            log.debug("错误列表: {}", request.getNames());

            return errorService.countError(request.getNames());

        } catch (Exception e) {
            log.error("批量处理错误信息失败: ", e);
            return R.error(AppHttpCodeEnum.ERROR).message("系统错误: " + e.getMessage());
        }
    }

    /**
     * 获取错误计数
     * @param timeRange
     * @return
     */
    @GetMapping("/countInfo/{timeRange}")
    public R countError(@PathVariable TimeRange timeRange) {
        return errorService.countInfo(timeRange);
    }


}